package com.example.bluetoothmultimeter;

import java.util.ArrayList;
import java.util.Set;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.graphics.Color;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	ArrayAdapter<String> listAdapter;
	Button connectNew;
	ListView pList;
	BluetoothAdapter btAdapter;
	Set<BluetoothDevice> devicesArray;
	ArrayList<String> pairedDevices;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
		if(btAdapter == null){
			Toast.makeText(getApplicationContext(), "No bluetooth", 0).show();
			finish();
		}
		else{
			if(!btAdapter.isEnabled()){
				Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
				startActivityForResult(intent,1);
			}
			getPairedDevices();
		}
	}

	private void getPairedDevices() {
		devicesArray = btAdapter.getBondedDevices();
		if(devicesArray.size() > 0){
			Toast.makeText(getApplicationContext(), "Showing paired devices.", Toast.LENGTH_SHORT).show();
			for(BluetoothDevice device:devicesArray){
				pairedDevices.add(device.getName()+"\n"+device.getAddress());
				listAdapter.add(device.getName()+"\n"+device.getAddress());
			}
		} else {
			Toast.makeText(getApplicationContext(), "No devices detected.", Toast.LENGTH_SHORT).show();
		}
	}

	private void init() {
		connectNew=(Button)findViewById(R.id.bConnectNew);
		pList=(ListView)findViewById(R.id.listView);
		pList.setChoiceMode(android.widget.AbsListView.CHOICE_MODE_SINGLE);
		listAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,0);
		pList.setAdapter(listAdapter);
		btAdapter=BluetoothAdapter.getDefaultAdapter();
		pairedDevices = new ArrayList<String>();
		pList.setBackgroundColor(Color.GREEN);
		pList.setClickable(true);
		pList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
	        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
	            Object o = pList.getItemAtPosition(position);
	            String str=(String)o;//As you are using Default String Adapter
	            Toast.makeText(getApplicationContext(),str,Toast.LENGTH_SHORT).show();
	        }
	    });
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data){
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode == RESULT_CANCELED){
			Toast.makeText(getApplicationContext(), "Please Enable Bluetooth", Toast.LENGTH_SHORT).show();
			finish();
		}
	}
}
